const app = getApp(); //引入导航

var util = require('../../utils/common.js'); //引入公共JS


import { Yclist } from 'yclist.model.js';
var yclist = new Yclist();

var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');

var qqmapsdk;
var searchRequest;

function selectNav() {
  //下拉菜单
  return ['hidden', 'hidden'];
}

var res3 = [];
Page({
  data: {
    cityData: "全国",
    scrollLeft: "",
    firstPerson1: '综合排序',
    firstPerson2: '全部时间',
    MBFlag: "true",
    SearchClose: true,
    //关闭搜索按钮
    SearchTo: false,
    //搜索按钮
    _num: '',
    //二级菜单
    tab_num: "",
    //一级菜单
    selectnav: selectNav(),
    cityFlag: true,
    //城市
    mengbanFlag: true,
    //蒙版
    searchFlag: true,
    //搜索
    wxcity: '',
    wxprovince: '',
    kefuphoneArr: []
  },
  onLoad: function (options) {
    console.log(options);
    this.getwxcity(options); // 获取客服手机号

    let kefuphone = tt.getStorageSync('kefuphoneArr');

    if (!kefuphone) {
      util.kefuphone();
    }

    this.setData({
      kefuphoneArr: kefuphone.slice(0, 2)
    });
  },
  //获取微信定位位置
  getwxcity: function (options) {
    qqmapsdk = new QQMapWX({
      key: '5XNBZ-SFPRU-M35VS-BNF67-DIHHJ-AEFD7' //这里自己的key秘钥进行填充

    }); //获取当时用户小程序设置（权限设置状态）

    tt.getSetting({
      success: res => {
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          this.getLocation(options);
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          this.getLocation(options);
        } else {
          this.getLocation(options);
        }
      }
    });
  },
  // 微信获得经纬度
  getLocation: function (options) {
    let vm = this;
    tt.getLocation({
      type: 'wgs84',
      success: function (res) {
        var latitude = res.latitude;
        var longitude = res.longitude;
        vm.getLocal(latitude, longitude, options);
      },
      fail: function (res) {
        console.log('fail' + JSON.stringify(res));

        vm._onLoad(options);

        vm.setData({
          wxcity: '全国'
        });
      }
    });
  },
  // 获取当前地理位置
  getLocal: function (latitude, longitude, options) {
    let vm = this;
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude
      },
      success: function (res) {
        let province = res.result.ad_info.province;
        let city = res.result.ad_info.city; //设置缓存（吧用户地址信息存入缓存）

        tt.setStorageSync('wxcity', city);
        tt.setStorageSync('wxprovince', province);

        vm._onLoad(options);

        vm.setData({
          wxcity: city
        });
      },
      fail: function (res) {
        console.log("error");

        vm._onLoad(options);

        this.setData({
          wxcity: '全国'
        });
      },
      complete: function (res) {}
    });
  },
  _onLoad: function (options) {
    var source = options.source;

    if (source) {
      tt.setStorageSync('source', source);
    }

    this.data.wxcity = tt.getStorageSync('wxcity');
    this.data.wxprovince = tt.getStorageSync('wxprovince');

    if (!this.data.wxcity) {
      this.data.wxcity = '';
      this.data.wxprovince = '';
    }

    tt.setStorageSync('options', options);

    if (options._city) {
      var _city = options._city;
      var _typeid1 = options._typeid1;
    } else {
      var _city = tt.getStorageSync('_city');

      var _typeid1 = tt.getStorageSync('_typeid1');
    }

    var _typeid = tt.getStorageSync('_typeid');

    var _cateId = tt.getStorageSync('_cateId'); //获取首页分类传的参数定位到分类


    if (_city == '' && _cateId == '') {
      var typeid1 = '0';
      var typeid2 = '0';
      var typeid3 = '0';
      var typeid4 = '0';
      this.setData({
        cityData: tt.getStorageSync('wxcity'),
        'typeid': typeid1,
        'typeid2': typeid2,
        'typeid3': typeid3
      });
      var shuliang = 6;
      tt.setStorageSync('shuliang', shuliang);
      tt.setStorageSync('typeid1', typeid1);
      tt.setStorageSync('typeid2', typeid2);
      tt.setStorageSync('typeid3', typeid3);
      tt.setStorageSync('typeid4', typeid4);
      this.data.typeid1 = typeid1;
      yclist.getSousuo(this.data.typeid1, this.data.typeid2, this.data.wxcity, this.data.wxprovince, res => {
        tt.setStorageSync('res1', res);
        this.setData({
          'SousuoArr': res,
          'All': true
        });
      }); //获取总条数

      yclist.getSousuoCount(this.data.typeid1, this.data.typeid2, this.data.wxcity, this.data.wxprovince, res => {
        tt.setStorageSync('nums', res.nums);
      });
    } else if (_city != '') {
      if (_typeid1) {
        var typeid1 = _typeid1;
        var typeid2 = options.typeid2;
      } else {
        var typeid1 = _typeid;
        var typeid2 = '0';
      }

      var typeid3 = '0';
      var typeid4 = '0';
      this.setData({
        'cityData': _city,
        'typeid': typeid1,
        'typeid2': typeid2,
        'typeid3': typeid3
      });
      var shuliang = 6;
      tt.setStorageSync('shuliang', shuliang);
      tt.setStorageSync('typeid1', typeid1);
      tt.setStorageSync('typeid2', typeid2);
      tt.setStorageSync('typeid3', typeid3);
      tt.setStorageSync('typeid4', typeid4);
      this.data.typeid1 = typeid1;
      this.data.typeid2 = typeid2;
      yclist.getSousuo(this.data.typeid1, this.data.typeid2, this.data.wxcity, this.data.wxprovince, res => {
        tt.setStorageSync('res1', res);
        this.setData({
          'SousuoArr': res,
          'All': true
        });
      }); //获取总条数

      yclist.getSousuoCount(this.data.typeid1, this.data.typeid2, this.data.wxcity, this.data.wxprovince, res => {
        tt.setStorageSync('nums', res.nums);
      });
    } else if (_cateId != '') {
      var _cateList = tt.getStorageSync('_cateList'); //获取首页分类传的参数滚动到具体某个分类


      console.log('首页分类跳转', _cateList);

      if (_cateList == 6) {
        this.setData({
          scrollLeft: 670
        });
      } // for (var i = 0; i <= 12; i++) { //判断传的值等于哪个下标使页面向左滚动到具体的距离
      //   if (i == _cateList) {
      //     var _width = (750 / 14) * i;
      //     this.setData({
      //       scrollLeft: _width
      //     });
      //     break;
      //   }
      // }


      var typeid = '0_' + _cateId + '_0_0';
      var shuliang = '0';
      var shuliang2 = '6';
      var typeid1 = typeid.split('_')[0];
      var typeid2 = typeid.split('_')[1];
      var typeid3 = typeid.split('_')[2];
      var typeid4 = typeid.split('_')[3];
      tt.setStorageSync('shuliang', shuliang2);
      tt.setStorageSync('typeid1', typeid1);
      tt.setStorageSync('typeid2', typeid2);
      tt.setStorageSync('typeid3', typeid3);
      tt.setStorageSync('typeid4', typeid4);
      this.data.typeid1 = typeid1, this.data.typeid2 = typeid2, this.data.typeid3 = typeid3, this.data.typeid4 = typeid4, this.data.shuliang = shuliang, this.data.shuliang2 = shuliang2, yclist.getPaixu(this.data.typeid1, this.data.typeid2, this.data.typeid3, this.data.typeid4, this.data.shuliang, this.data.shuliang2, this.data.wxcity, this.data.wxprovince, res => {
        tt.setStorageSync('res1', res);
        this.setData({
          'SousuoArr': res,
          'typeid': this.data.typeid1,
          'typeid2': this.data.typeid2,
          'typeid3': this.data.typeid3,
          'All': true
        });
      });
      yclist.getPaixuCount(this.data.typeid1, this.data.typeid2, this.data.typeid3, this.data.typeid4, this.data.shuliang, this.data.shuliang2, res => {
        tt.setStorageSync('nums', res.nums);
      });
    }
  },
  //关闭页面时隐藏城市弹框,关闭一级和二级菜单
  onHide: function () {
    //点击关闭二级列表
    var newselect = selectNav();
    newselect[0] = 'hidden';
    newselect[1] = 'hidden';
    this.setData({
      searchFlag: true,
      //关闭搜索列表
      value: "",
      //清空输入框的值
      SearchClose: true,
      SearchTo: false,
      firstPerson1: '综合排序',
      //二级菜单默认显示综合排序
      firstPerson2: '全部时间',
      //二级菜单默认显示全部时间
      selectnav: newselect,
      //隐藏二级列表
      MBFlag: true,
      //关闭蒙版
      scrollLeft: 0,
      //一级菜单左侧位移为0
      tab_num: "" //二级菜单默认显示综合排序

    });
    tt.removeStorageSync("_typeid"); //清空缓存

    tt.removeStorageSync("typeid1"); //清空缓存

    tt.removeStorageSync("typeid2"); //清空缓存

    tt.removeStorageSync("typeid3"); //清空缓存

    tt.removeStorageSync("typeid4"); //清空缓存

    tt.removeStorageSync("_city"); //清空缓存

    tt.removeStorageSync("_cateId"); //清空缓存
    // var options = wx.getStorageSync('options');
    // this.onLoad(options); //打开页面重新加载
  },
  listClick: function (e) {
    var _typeid1 = e.target.dataset.typeid.split('_')[0];
    var _typeid2 = e.target.dataset.typeid.split('_')[1];
    var _typeid3 = e.target.dataset.typeid.split('_')[2];
    var _typeid4 = e.target.dataset.typeid.split('_')[3];
    var shuliang = 6;
    var value = ""; //点击关闭二级列表

    var newselect = selectNav();
    newselect[0] = 'hidden';
    newselect[1] = 'hidden';
    tt.setStorageSync('shuliang', shuliang);
    tt.setStorageSync('typeid1', _typeid1);
    tt.setStorageSync('typeid2', _typeid2);
    tt.setStorageSync('typeid3', _typeid3);
    tt.setStorageSync('typeid4', _typeid4);
    this.data.typeid1 = _typeid1, this.data.typeid2 = _typeid2, this.data.typeid3 = _typeid3, this.data.value = value, yclist.getFenLei(this.data.typeid1, this.data.typeid2, this.data.wxcity, this.data.wxprovince, res => {
      tt.setStorageSync('res1', res);
      this.setData({
        'SousuoArr': res,
        'value': value,
        'All': true,
        'typeid': this.data.typeid1,
        'typeid2': this.data.typeid2,
        'typeid3': this.data.typeid3,
        firstPerson1: '综合排序',
        firstPerson2: '全部时间',
        selectnav: newselect,
        MBFlag: true,
        //关闭蒙版
        _num: "",
        tab_num: ""
      });
    });
    yclist.getFenLeiCount(this.data.typeid1, this.data.typeid2, this.data.wxcity, this.data.wxprovince, res => {
      tt.setStorageSync('nums', res.nums);
    });
  },
  //打开选择城市
  onChangeCity: function (e) {
    //选择城市时关闭二级菜单
    var newselect = selectNav();
    newselect[0] = 'hidden';
    newselect[1] = 'hidden';
    this.setData({
      cityFlag: false,
      searchFlag: true,
      //关闭搜索列表
      value: "",
      //清空输入框的值
      SearchClose: true,
      SearchTo: false,
      'selectnav': newselect,
      //关闭二级菜单
      'MBFlag': true //关闭蒙版

    });
  },
  //选择城市
  citySelect: function (e) {
    var _city = e.target.dataset.city;
    var typeid1 = e.target.dataset.typeid;
    this.data.typeid1 = typeid1;
    var shuliang = 6;
    var value = "";
    var typeid2 = '0';
    var typeid3 = '0';
    var typeid4 = '0';

    var _typeid = typeid1 + '_0_0_0';

    tt.setStorageSync('_typeid', _typeid);
    tt.setStorageSync('_city', _city);
    tt.setStorageSync('typeid1', typeid1);
    tt.setStorageSync('typeid1', typeid1);
    tt.setStorageSync('typeid2', typeid2);
    tt.setStorageSync('typeid3', typeid3);
    tt.setStorageSync('typeid4', typeid4);
    tt.setStorageSync('shuliang', shuliang);
    yclist.getSousuo(this.data.typeid1, res => {
      tt.setStorageSync('res1', res);
      this.setData({
        'value': value,
        'SousuoArr': res,
        'typeid': typeid1,
        'typeid1': '0',
        cityData: _city,
        cityFlag: true,
        'All': true,
        'typeid2': "",
        scrollLeft: 0,
        //一级菜单左侧位移为0
        tab_num: "",
        firstPerson1: '综合排序',
        firstPerson2: '全部时间'
      });
    });
  },
  //打开关闭城市
  onCloseCity: function (e) {
    this.setData({
      cityFlag: true
    });
  },
  searchValue: function (e) {
    //打开搜索
    var _data = e.detail.value;
    this.data.SearchValue = e.detail.value;
    var patt3 = /^[\u4e00-\u9fa5]{2,20}$/; //正则验证汉字不能少两个，不能多余20个

    this.setData({
      //保存输入框的值
      value: e.detail.value
    });
    this.data.datarow = _data;
    var that = this;
    clearTimeout(searchRequest);
    searchRequest = setTimeout(() => {
      if (_data == '') {
        that.setData({
          searchFlag: true,
          SearchClose: true,
          SearchTo: false,
          value: "" //清空输入框的值

        });
      } else if (patt3.test(_data)) {
        //搜索页面
        yclist.getSearchFor1(that.data.datarow, res => {
          that.setData({
            'SearchFor': res,
            searchFlag: false,
            SearchClose: false,
            SearchTo: true
          });
        });
      }
    }, 1000);
  },
  //使用回车键搜索
  searchEnter: function () {
    this.setData({
      searchFlag: true,
      SearchClose: true,
      SearchTo: false,
      value: "" //清空输入框的值

    });
    var inputdata = this.data.SearchValue;

    if (inputdata) {
      tt.navigateTo({
        url: '../ycSearch/ycSearch?inputdata=' + inputdata
      });
    }
  },
  //点击关闭按钮关闭搜索框
  toClose: function () {
    this.setData({
      searchFlag: true,
      SearchClose: true,
      SearchTo: false,
      value: "" //清空输入框的值

    });
  },
  //关闭搜索
  onCloseSearch: function () {
    this.setData({
      searchFlag: true,
      SearchClose: true,
      SearchTo: false,
      value: "" //清空输入框的值

    });
  },
  clickTab: function (e) {
    //点击列表切换
    var index = parseInt(e.currentTarget.dataset.index);
    var newselect = selectNav();

    if (this.data.selectnav[index] == 'hidden') {
      newselect[index] = 'show';
      this.setData({
        MBFlag: false,
        //打开蒙版
        tab_num: e.target.dataset.tabnum
      });
    } else {
      newselect[index] = 'hidden';
      this.setData({
        MBFlag: true,
        //关闭蒙版
        tab_num: e.target.dataset.tabnum
      });
    } //设置为新的数组


    this.setData({
      selectnav: newselect
    });
  },
  mySelect: function (e) {
    //点击列表选择
    var index = parseInt(e.currentTarget.dataset.index);
    var newselect = selectNav();
    newselect[index] = 'hidden';
    var index2 = e.currentTarget.dataset.index.split('-')[0]; //--------------------------------全部选择时间

    var _typeid1 = e.target.dataset.typeid.split('_')[0];
    var _typeid2 = e.target.dataset.typeid.split('_')[1];
    var _typeid3 = e.target.dataset.typeid.split('_')[2];
    var _typeid4 = e.target.dataset.typeid.split('_')[3];
    var shuliang = 0;
    var shuliang2 = 6;
    var value = "";
    tt.setStorageSync('typeid1', _typeid1);
    tt.setStorageSync('typeid2', _typeid2);
    tt.setStorageSync('typeid3', _typeid3);
    tt.setStorageSync('typeid4', _typeid4);
    tt.setStorageSync('shuliang', shuliang2);
    this.data.shuliang = shuliang, this.data.shuliang2 = shuliang2, this.data.typeid1 = _typeid1, this.data.typeid2 = _typeid2, this.data.typeid3 = _typeid3, this.data.typeid4 = _typeid4, this.data.value = _typeid4, yclist.getPaixu(this.data.typeid1, this.data.typeid2, this.data.typeid3, this.data.typeid4, this.data.shuliang, this.data.shuliang2, this.data.wxcity, this.data.wxprovince, res => {
      tt.setStorageSync('res1', res);
      this.setData({
        'SousuoArr': res,
        'value': value,
        'typeid': this.data.typeid1,
        'typeid2': this.data.typeid2,
        'typeid3': this.data.typeid3,
        'All': true
      });
    });
    yclist.getPaixuCount(this.data.typeid1, this.data.typeid2, this.data.typeid3, this.data.typeid4, this.data.shuliang, this.data.shuliang2, res => {
      tt.setStorageSync('nums', res.nums);
    }); //-------------------------------------

    if (index2 == 0) {
      this.setData({
        selectnav: newselect,
        firstPerson1: e.target.dataset.text,
        MBFlag: true,
        //关闭蒙版
        _num: e.target.dataset.num
      });
    } else if (index2 == 1) {
      this.setData({
        selectnav: newselect,
        firstPerson2: e.target.dataset.text,
        MBFlag: true,
        //关闭蒙版
        _num: e.target.dataset.num
      });
    }
  },
  tapMenuHide: function (e) {
    //点击蒙版关闭蒙版和二级菜单
    var index = parseInt(e.currentTarget.dataset.index);
    var newselect = selectNav();
    newselect[index] = 'hidden';
    this.setData({
      selectnav: newselect,
      MBFlag: true //关闭蒙版

    });
  },
  // 拨打电话
  calling: function () {
    util.calling();
  },
  //分享
  onShareAppMessage: function () {
    util.sharePage();
  },
  // 获取滚动条当前位置
  onPageScroll: function (e) {
    if (e.scrollTop > 100) {
      this.setData({
        floorstatus: true
      });
    } else {
      this.setData({
        floorstatus: false
      });
    }
  },
  //回到顶部
  goTop: function () {
    util.goTop();
  },
  //上拉加载
  onReachBottom: function (e) {
    var shuliang = tt.getStorageSync("shuliang");
    var shuliang2 = shuliang + 5;
    tt.setStorageSync('shuliang', shuliang2);
    this.data.shuliang = shuliang;
    this.data.shuliang2 = shuliang2;

    var _typeid1 = tt.getStorageSync("typeid1");

    var _typeid2 = tt.getStorageSync("typeid2");

    var _typeid3 = tt.getStorageSync("typeid3");

    var _typeid4 = tt.getStorageSync("typeid4");

    var nums = tt.getStorageSync("nums");
    var value = "";
    this.data.typeid1 = _typeid1, this.data.typeid2 = _typeid2, this.data.typeid3 = _typeid3, this.data.typeid4 = _typeid4, this.data.value = value;
    console.log(nums, shuliang2);

    if (nums - shuliang2 >= 0) {
      yclist.getPaixu(this.data.typeid1, this.data.typeid2, this.data.typeid3, this.data.typeid4, this.data.shuliang, this.data.shuliang2, this.data.wxcity, this.data.wxprovince, res => {
        var res1 = tt.getStorageSync("res1");
        var res2 = res1.concat(res); // 遍历id在存入新数组中

        var res4 = [];

        for (var i in res2) {
          res4.push(res2[i]['id']);
        } // 循环遍历去重id


        for (var i = 0; i < res4.length; i++) {
          if (res3.indexOf(res4[i]) === -1) {
            res3.push(res4[i]);
          }
        } // 循环去重


        var newarr = []; // 新建空数组

        for (var i in res2) {
          // 遍历原数组
          var isadd = true; //设置标记

          for (var j in newarr) {
            //遍历新数组
            if (res2[i]['id'] === newarr[j]['id']) {
              //如果当前原数组中的项，在新数组中存在，则标记为‘不添加’，并跳出新数组遍历
              isadd = false;
              break;
            }
          }

          if (isadd) {
            newarr.push(res2[i]); //若标记为‘添加’（即遍历新数组后，没有发现相同项），则添加进新数组
          }
        }

        res2 = newarr; // 重新赋值到res2

        tt.setStorageSync('res1', res2);
        this.setData({
          'SousuoArr': res2,
          'value': value,
          'typeid': this.data.typeid1,
          'typeid2': this.data.typeid2,
          'typeid3': this.data.typeid3,
          'All': true
        });
      });
      tt.showLoading({
        title: '加载中'
      });
    } else {
      tt.showLoading({
        title: '已经加载完了'
      });
      this.setData({
        'All': false
      });
    } //显示.5秒隐藏加载提示


    setTimeout(function () {
      tt.hideLoading();
    }, 500);
  }
});